using UnityEngine;
using System;
using System.Collections;
using System.Net;
using System.Net.Sockets;
using System.Text;
public class CSdListener_TCP : CsdSocket_TCP {

    //public CsdSocket_TCP[] clients = new CsdSocket_TCP[5];
    public ArrayList clients = new ArrayList();
    bool started = false;
    CProtocolMgr proMgr = null;
    public bool StartListen(string ip, int port, CProtocolMgr pm)
    {
        try
        {
            if (started == false)
            {
                if (pm == null)
                {
                    return false;
                }
                proMgr = pm;
                // create the socket
                s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                // bind the listening socket to the port
	            IPAddress hostIP = IPAddress.Parse(ip);
                IPEndPoint ep = new IPEndPoint(hostIP, port);
                s.Bind(ep); 
                // start listening
                s.Listen(5);
                s.Blocking = false;
                started = true;
                UnityEngine.Debug.Log("start listen in " + ip + " :" + port);
                return true;
            }
        }
        catch (System.Exception ex)
        {
            UnityEngine.Debug.Log("listen failed" + ex.Message);
        }
        return false;
    }
    public int HandleClients()
    {
        try
        {
            foreach (CsdSocket_TCP client in clients)
            {
                //recv 
                if (client.s.Connected == false)
                {
                    clients.Remove(client);
                    break;
                }
                int recvcount = 0;
                byte[] recvbuf = client.RecvAndReturn(ref recvcount);
                if (recvbuf != null && proMgr != null)
                {

                    int movetohead = proMgr.Handle(recvbuf, recvcount, client);
                    while (movetohead > 0 && recvcount >=0)
                    {
                        client.SetRecvIndex(movetohead);
                        recvcount -= movetohead;
                        movetohead = proMgr.Handle(recvbuf, recvcount, client);

                    }
                }
                else
                {
                }
            }
        }
        catch (System.Exception ex)
        {
            UnityEngine.Debug.Log("remove bad client errrrrr" + ex.Message);
        }
        return 1;
    }
    public int HandleAccept()
    {
        if (clients.Count < 10)
        {
            try
            {
                Socket _c = s.Accept();
                _c.Blocking = false;
                CsdSocket_TCP _socket = new CsdSocket_TCP();
                _socket.Attach(_c);
                clients.Add(_socket);
                UnityEngine.Debug.Log("add socket");
            }
            catch (System.Exception)
            {
                //UnityEngine.Debug.Log("no clients coming");
            }
        }
        //print("handle accept");
        return 1;
    }
    public int Run()
    {
        HandleAccept();
        HandleClients();
        return 1;
    }
}


public class CMyProtocolMgr : CProtocolMgr
{
    public server_cs cs = null;
    public CMyProtocolMgr()
    { 
        GameObject respawn = GameObject.FindWithTag("servercs");
        cs = respawn.GetComponent<server_cs>();
    }

    public override void handle_PKT_GSCLI_TRIGGER_ACTION_ACK(PKT_GSCLI_TRIGGER_ACTION_ACK p, CsdSocket_TCP peer)
    {
        UnityEngine.Debug.Log("1111111server recv msg from :" + peer.HandleID());
        cs.lable_desc += "1111111server recv msg from :" + peer.HandleID() + "\n";
        //p.DebugPrint();
    }
    public override void handle_PKT_CLIGS_TRIGGER_DAMAGE_REQ(PKT_CLIGS_TRIGGER_DAMAGE_REQ p, CsdSocket_TCP peer) 
    {
        UnityEngine.Debug.Log("22222222222server recv msg from :" + peer.HandleID());
        //p.DebugPrint();
    }
    public override void handle_PKT_CLIGS_SKILL_EVENT_REQ(PKT_CLIGS_SKILL_EVENT_REQ p, CsdSocket_TCP peer)
    {
        UnityEngine.Debug.Log("3333333server recv msg from :" + peer.HandleID());
        //p.DebugPrint();
    }


}